﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Xml.Serialization;

namespace SvgApplication.Authorization
{
    public partial class UserData
    {
        public int UserID { get; set; }
        public string UserName { get; set; }
        public string UserToken { get; set; }

        public UserData()
        {

        }

        public UserData(int userID, string userName)
        {
            this.UserID = userID;
            this.UserName = userName;
        }

        /// <summary>
        /// serializes the content of the class into XML format
        /// </summary>
        /// <returns>the serialized string</returns>
        public string ToXml()
        {
            StringWriter sw = new StringWriter();
            XmlSerializer serializer = new XmlSerializer(typeof(UserData));
            serializer.Serialize(sw, this);
            sw.Close();

            return sw.ToString();
        }

        /// <summary>
        /// creates a UserData object from a given XML
        /// </summary>
        /// <param name="userDataXml">the input XML</param>
        /// <returns>the UserData object</returns>
        public static UserData CreateUserData(string userDataXml)
        {
            try
            {
                if (string.IsNullOrEmpty(userDataXml))
                {
                    return null;
                }
                else
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(UserData));
                    UserData ud = (UserData)serializer.Deserialize(new StringReader(userDataXml));
                    return ud;
                }
            }
            catch
            {
                return null;
            }
        }
    }

}